package com.cn.wanxi.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.entity.CompanyEntity;
import com.cn.wanxi.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/back/company")
public class CompanyController {

    @Autowired
    private ICompanyService iCompanyService;

    @RequestMapping("/findAll")
    public void findAll(CompanyEntity companyEntity, HttpServletResponse resp) throws IOException {

        List<CompanyEntity> list = iCompanyService.findAll(companyEntity);
        int count = iCompanyService.getCount(companyEntity);//得到分页总条数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("count",count);
        resp.getWriter().println(jsonObject);
    }

    @RequestMapping("/del")
    public void del(CompanyEntity companyEntity, HttpServletResponse resp) throws IOException {

        int result = iCompanyService.del(companyEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        resp.getWriter().println(jsonObject);
    }

    @RequestMapping("/findById")
    public void findById(CompanyEntity companyEntity, HttpServletResponse resp) throws IOException {

        CompanyEntity model = iCompanyService.findById(companyEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model",model);
        resp.getWriter().println(jsonObject);
    }

    @RequestMapping("/edit")
    public void edit(CompanyEntity companyEntity, HttpServletResponse resp) throws IOException {

        String result = iCompanyService.edit(companyEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }

    @RequestMapping("/add")
    public void add(CompanyEntity companyEntity, HttpServletResponse resp) throws IOException {

        String result = iCompanyService.add(companyEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
}
